package servlet.hou;

import bean.HygInfo;
import bean.PageRequest;
import bean.Result;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import utils.DBUtil;
import utils.JwtUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


//添加卫生信息
@WebServlet("/hou/addHygByHou")
public class AddHygByHouServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Integer dorNumber= Integer.valueOf(request.getParameter("dorNumber"));
        Integer checkSore= Integer.valueOf(request.getParameter("checkSore"));
        String  hygDes= request.getParameter("hygDes");
        String user_token = request.getHeader("user_token_header");
        Integer buildId = JwtUtils.getHouBuildIdFromToken(user_token,"buildId");
        Integer doId = JwtUtils.getHouIdFromToken(user_token,"houId");
        String check_person = JwtUtils.getHouNameFromToken(user_token,"houName");
        HygInfo hygInfo = queryHygBuildDor(buildId,dorNumber);
        Integer data= null;
        try {
            data = UserDaoImpl.updateHygByHou(hygInfo.getHygId(), hygDes, dorNumber, checkSore,doId,check_person);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//       Integer data= UserDaoImpl.addHygByHou(dorNumber,checkSore,hygDes);
        Result result = new Result();
        if(data!=0){
            result.success(data);
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }else{
            result.fail();
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);


        }




    }

    private HygInfo queryHygBuildDor(Integer buildId, Integer dorNumber) {
        PageRequest pageRequest = new PageRequest();

        Connection connection=null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select * from hyg where delete_flag=0 and build_id=? and dor_number = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,buildId);
            preparedStatement.setInt(2,dorNumber);
            resultSet = preparedStatement.executeQuery();
            HygInfo hygInfo=new HygInfo();
            while (resultSet.next()){

                hygInfo.setHygId(resultSet.getInt("hyg_id"));
                hygInfo.setBuildId(resultSet.getInt("build_id"));
                hygInfo.setDorNumber(resultSet.getInt("dor_number"));
                hygInfo.setCheckSore(resultSet.getInt("check_sore"));
                hygInfo.setCheckPerson(resultSet.getString("check_person"));
                hygInfo.setDoId(resultSet.getInt("do_id"));
                hygInfo.setHygDes(resultSet.getString("hyg_des"));
                hygInfo.setDeleteFlag(resultSet.getInt("delete_flag"));
                hygInfo.setCreateTime(resultSet.getDate("create_time"));
                hygInfo.setUpdateTime(resultSet.getDate("update_time"));
                hygInfo.setCheckTime(resultSet.getDate("check_time"));

            }
            return hygInfo;


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
